<?php
/**
* 线图多线条
*
 * PHP version 5
 *
 * @package   Ecga
 * @author    Farmer.Li <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   SVN: $Id$
 */
namespace Project\Apps\Ecga\Library\FusionCharts;

use \Project\Apps\Ecga\Library\FusionCharts\ChartXml;
use \Exception;

/**
* 线图多线条
*
 * PHP version 5
 *
 * @package   Ecga
 * @author    Farmer.Li <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: <0.2>
 */
class MSLine extends ChartXml
{
    /**
     * 数据缓存数组
     */
    protected $tmpData = array();
    
    /**
     * 构造函数
     * 
     * @param string $caption 图表说明
     * 
     * @return void
     */
    public function __construct($caption)
    {
        parent::__construct($caption);
        $this->setStyle(
            array(
                'numdivlines' => '9',
                'lineThickness' => '2',
                'showValues' => '0',
                'numVDivLines' => '22',
                'formatNumberScale' => '1',
                'labelDisplay' => 'ROTATE',
                'slantLabels' => '1',
                'anchorRadius' => '2',
                'anchorBgAlpha' => '50',
                'showAlternateVGridColor' => '1',
                'anchorAlpha' => '100',
                'animation' => '1',
                'limitsDecimalPrecision' => '0',
                'divLineDecimalPrecision' => '1'
            )
        );
    }
    
    /**
     * 设置图表数据
     *
     * @param array  $data  格式化数组
     * @param string $color 颜色
     * 
     * @return object
     */
    public function setData($data = array(), $color = '')
    {
        if (!$data) {
            return false;
        }
        foreach ($data as $key => $value) {
            $this->tmpData[$key] = array(
                'color' => $color ? $color : $this->randColor(),
                'showValues' => 0,
                'data' => $value
            );
        }
    }
    
    /**
     * 格式化数据
     * 
     * @return void
     */
    protected function format()
    {
        $this->data['__child']['categories'] = array(
            'tagName' => 'categories'
        );
        $tmpCate = current($this->tmpData);
        $categories = array_keys($tmpCate['data']);
        foreach ($categories as $category) {
            $this->data['__child']['categories']['__child'][$category] = array(
                'tagName' => 'category',
                '__attr' => array(
                    'label' => $category
                )
            );
        }
        foreach ($this->tmpData as $service => $datas) {
            $tmp = array(
                'tagName' => 'dataset',
                '__attr' => array(
                    'seriesName' => $service,
                    'color' => $datas['color'],
                    'showValues' => $datas['showValues'],
                ),
            );
            foreach ($categories as $category) {
                $tmp['__child'][] = array(
                    'tagName' => 'set',
                    '__attr' => array(
                        'value' => $datas['data'][$category]
                    )
                );
            }
            $this->data['__child'][$service] = $tmp;
        }
        if ($this->tmpData['LINE']) {
            foreach ($this->tmpData['LINE']['data'][0] as $line) {
                self::trendLines($line);
            }
        }
        if ($this->tmpData['SITELINE']) {
            self::siteLine($this->tmpData['SITELINE']['data'][0]);
        }
    }
    /**
     * 增加固定的标线三条
     *
     * @param string $line 标线名称
     *
     * @return array
     */
    public function trendLines($line)
    {
        $temp = explode(':', $line);
        $tmp = array(
            'tagName' => 'trendLines',
        );
        $tmp['__child'][] = array(
            'tagName' => 'line',
            '__attr' => array( 
                'color' => '009933',
                'startValue' => floatval($temp[1]),
                'displayvalue' => $temp[0] .'    '
            )
        );
        $this->data['__child'][$temp[0]] = $tmp;
        unset($this->data['__child']['LINE']);
    }
    /**
     * 增加固定的标线一条
     *
     * @param string $siteLine 标线名称
     *
     * @return array
     */
    public function siteLine($siteLine)
    {
        $tmp = array(
            'tagName' => 'trendLines',
        );
        $tmp['__child'][] = array(
            'tagName' => 'line',
            '__attr' => array( 
                'color' => '009933',
                'startValue' => 1,
                'displayvalue' => $siteLine.'    '
            )
        );
        $this->data['__child']['SITELINE'] = $tmp;
    }
}
?>